package com.ease.framework.gef4p.editparts {
   import com.ease.framework.gef.IEditPart;
   import com.ease.framework.gef.IEditor;
   import com.ease.framework.gef4g.GraphicalViewport;
   import com.ease.framework.gef4g.IGraphicalEditor;
   import com.ease.framework.gef4g.IGraphicalViewer;
   import com.ease.framework.gef4p.figures.impl.edge.EdgeFigure;
   import com.ease.framework.gef4p.model.Annotation;
   import com.ease.framework.gef4p.model.Edge;
   import com.ease.framework.gef4p.model.Node;
   import com.ease.framework.gef4p.model.Pool;
   import com.ease.framework.workbench.Workbench;
   import com.ease.framework.workbench.spi.ICommand;
   import com.ease.framework.workbench.spi.impls.CompoundCommand;

   import mx.collections.ArrayCollection;
   import mx.core.UIComponent;

   public class PoolEditPart extends AnnotationContainerEditPart {
      protected var activities:ArrayCollection = new ArrayCollection();
      protected var transitions:ArrayCollection = new ArrayCollection();

      public function PoolEditPart(model:Pool) {
         super(model);
      }

      public function addNodeEditPart(act:NodeEditPart):void {
         var viewport:GraphicalViewport = GraphicalViewport(IGraphicalEditor(Workbench.instance.currentEditor).getGraphicalViewer().getViewport());
         activities.addItem(act);
         figure.addChild(act.figure as UIComponent);
         act.refreashFigure();

         if (act.figure.x + act.figure.width > viewport.width)
            viewport.width = act.figure.x + act.figure.width + 50;
         if (act.figure.y + act.figure.height > viewport.height)
            viewport.height = act.figure.y + act.figure.height + 50;
      }

      public function removeNodeEditPart(act:NodeEditPart):void {
         var viewport:GraphicalViewport = GraphicalViewport(IGraphicalEditor(Workbench.instance.currentEditor).getGraphicalViewer().getViewport());
         if (activities.contains(act)) {
            activities.removeItemAt(activities.getItemIndex(act));
         }
         UIComponent(figure).removeChild(act.figure as UIComponent);
      }

      public function addEdgeEditPart(tran:EdgeEditPart):void {
         transitions.addItem(tran);

         var viewport:GraphicalViewport = GraphicalViewport(IGraphicalEditor(Workbench.instance.currentEditor).getGraphicalViewer().getViewport());

         viewport.connectionLayer.addChild(tran.figure as UIComponent);
         //将EdgeAnchor放置于HandlesLayer
         viewport.handlesLayer.addChild(EdgeFigure(tran.figure).stManager.sourceAnchor as UIComponent);
         viewport.handlesLayer.addChild(EdgeFigure(tran.figure).stManager.targetAnchor as UIComponent);

         tran.refreashFigure();
      }

      public function removeEdgeEditPart(tran:EdgeEditPart):void {
         if (transitions.contains(tran)) {
            transitions.removeItemAt(transitions.getItemIndex(tran));
            var viewport:GraphicalViewport = GraphicalViewport(IGraphicalViewer(IGraphicalEditor(Workbench.instance.currentEditor).getGraphicalViewer()).getViewport());

            viewport.handlesLayer.removeChild(EdgeFigure(tran.figure).stManager.sourceAnchor as UIComponent);
            viewport.handlesLayer.removeChild(EdgeFigure(tran.figure).stManager.targetAnchor as UIComponent);

            viewport.connectionLayer.removeChild(tran.figure as UIComponent);
         }
      }

      override public function createMoveResizeCommand(gap_x:Number, gap_y:Number, gap_width:Number, gap_height:Number):ICommand {
         var command:CompoundCommand = new CompoundCommand();

         for each (var act:Node in Pool(model).nodes) {
            var actEP:IEditPart = IEditor(Workbench.instance.currentEditor).getEditDomain().getEditPartByModel(act);
            command.addCommand(actEP.createMoveResizeCommand(gap_x, gap_y, gap_width, gap_height));
         }

         return command;
      }

      override public function reload():void {
         activities.removeAll();
         transitions.removeAll();
         annotations.removeAll();
         for each (var act:Node in Pool(model).nodes) {
            var actEP:IEditPart = IEditor(Workbench.instance.currentEditor).getEditPartFactory().createEditPart(act);
            addNodeEditPart(actEP as NodeEditPart);
         }

         for each (var tran:Edge in Pool(model).getEdges()) {

            var tranEP:IEditPart = IEditor(Workbench.instance.currentEditor).getEditPartFactory().createEditPart(tran);

            var sourceEP:NodeEditPart = IEditor(Workbench.instance.currentEditor).getEditDomain().getEditPartByModel(tran.source) as NodeEditPart;
            var targetEP:NodeEditPart = IEditor(Workbench.instance.currentEditor).getEditDomain().getEditPartByModel(tran.target) as NodeEditPart;

            EdgeEditPart(tranEP).setSource(sourceEP);
            EdgeEditPart(tranEP).setTarget(targetEP);

            addEdgeEditPart(tranEP as EdgeEditPart);
         }

         for each (var ann:Annotation in Pool(model).getArtifacts()) {
            var annEP:IEditPart = IEditor(Workbench.instance.currentEditor).getEditPartFactory().createEditPart(ann);
            addAnnotationEditPart(annEP as AnnotationEditPart);
         }
      }
   }
}